package com.tamtamy.jtta.utility;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONUtility {

	public static String getString(JSONObject jsonObject, String field){
		String value = null;
		if (jsonObject.get(field)!=null){
			value = jsonObject.getString(field);
		}
		return value;
	}
	
	public static long getLong(JSONObject jsonObject, String field){
		long value = 0;
		if (jsonObject.get(field)!=null){
			value = jsonObject.getLong(field);
		}
		return value;
	}	
	
	public static boolean getBoolean(JSONObject jsonObject, String field){
		boolean value = false;
		if (jsonObject.get(field)!=null){
			value = jsonObject.getBoolean(field);
		}
		return value;
	}	
	
	public static Date getDate(JSONObject jsonObject, String field) {
		Date creationDate = null;
		if (!jsonObject.getJSONObject(field).isNullObject()){
			creationDate = GregorianCalendar.getInstance().getTime();
			creationDate.setTime(Long.parseLong(jsonObject.getJSONObject(field).getString("time")));
		}
		return creationDate;
	}	
	
	public static ArrayList<String> getStringList(JSONObject jsonObject, String listName) {
		ArrayList<String> stringList = new ArrayList<String>();
		JSONArray listJSON = jsonObject.getJSONArray(listName);
		if (listJSON!=null){
			for (int i = 0; i < listJSON.size(); i++) {
				String stringItem = listJSON.getString(i);
				stringList.add(stringItem);
			}
		}
		return stringList;
	}
	
	
	
}
